﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class Dashboard : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    List<MatchSchedule> matchSchedules;
    Client client;
    
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                bindMatches();
                bindTopClients();
            }
        }
    }

    private void bindTopClients()
    {
        client = new Client(_dataObject);
        HttpCookie cookie = Request.Cookies["Admin"];
        if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.SuperAdmin))
        {
            List<GetTopClientForSuperAdminResult> lstResult = new List<GetTopClientForSuperAdminResult>();
            lstResult = client.GetTopClientForSuperAdmin();
            if(lstResult.Count > 0)
            {
                rptClient.DataSource = lstResult;
                rptClient.DataBind();
            }
            else
            {
                rptClient.DataSource = null;
                rptClient.DataBind();
            }
            
        }
        else if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.Admin))
        {
            List<GetTopClientForAdminResult> lstResult = new List<GetTopClientForAdminResult>();
            lstResult = client.GetTopClientForAdmin(Convert.ToInt32(cookie["AdminId"]));
            if (lstResult.Count > 0)
            {
                rptClient.DataSource = lstResult;
                rptClient.DataBind();
            }
            else
            {
                rptClient.DataSource = null;
                rptClient.DataBind();
            }
        }
    }

    private void bindMatches()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetUpComingMatchesResult> lstMatches = new List<GetUpComingMatchesResult>();
        lstMatches = matchSchedule.GetUpComingMatches();
        
        if (lstMatches.Count > 0)
        {
            rptMatch.DataSource = lstMatches;
            rptMatch.DataBind();
        }
        else
        {
            rptMatch.DataSource = null;
            rptMatch.DataBind();
        }
    }
}